<?php

namespace App\Tables;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $dates = [
        'paid_at', 'service_start_at', 'service_end_at'
    ];

    protected $casts = [
        'prorations' => 'json',
        'ladder_price' => 'json'
    ];

    protected $appends = [
        'status_text',
        'agent_name',
        'device_address',
        'username',
        'source_text',
        'payment'
    ];

    /**
     * 生成订单号
     * @return string
     */
    public function makeOrderSn()
    {
        return strtoupper(uniqid(date('Ymdh')));
    }

    /**
     * 获取订单支付状态
     * @return mixed|string
     */
    public function getStatusTextAttribute()
    {
        switch ($this->status) {
            case 'wait_pay':
                return '待付款';
            case 'paid':
                return '已付款';
            case 'refund':
                return '已退款';
            case 'service':
                return '服务中';
            case 'success':
                return '交易成功';
            case 'close':
                return '订单关闭';
            default:
                return $this->status;
        }
    }


    /**
     * 获取订单支付状态
     * @return mixed|string
     */
    public function getSourceTextAttribute()
    {
        switch ($this->status) {
            case 'web':
                return '网站';
            case 'ios':
                return 'IOS客户端';
            case 'android':
                return 'Android客户端';
            default:
                return $this->status;
        }
    }

    /**
     * 获取所属代理商名称
     * @return mixed
     */
    public function getAgentNameAttribute()
    {
        return $this->agent->realname;
    }

    /**
     * 获取所属代理商名称
     * @return mixed
     */
    public function getDeviceAddressAttribute()
    {
        return $this->device->full_address;
    }


    /**
     * 获取下单用户姓名
     * @return mixed
     */
    public function getUsernameAttribute()
    {
        if (!empty($this->user->nickname)) {
            return $this->user->nickname;
        } else if (!empty($this->user->name)) {
            return $this->user->name;
        }
    }

    /**
     * 获取下单用户信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->hasOne('App\Tables\User', 'id', 'user_id');
    }

    /**
     * 订单所属代理商
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function agent()
    {
        return $this->hasOne('App\Tables\User', 'id', 'agent_user_id');
    }

    /**
     * 设备信息
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function device()
    {
        return $this->hasOne('App\Tables\Device', 'device_sn', 'device_sn');
    }

    /**
     * 获取订单支付信息
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function getPaymentAttribute()
    {
        if (!isset($this->attributes['order_sn'])) {
            return [];
        }

        return  OrderPayment::where('status', 'paid')->where('order_sn', $this->order_sn)->get();
    }

    /**
     * 获取付款的支付单
     * @return $this
     */
    public function payments()
    {
        return $this->hasMany('App\Tables\OrderPayment', 'order_sn', 'order_sn')
            ->where('status', 'paid');
    }

    /**
     * 获取所有下级代理的订单
     * @param $query
     * @param $id
     * @return mixed
     */
    public function scopeOfChildren($query, $id)
    {
        return $query->whereIn('agent_user_id', function ($query) use ($id) {
            $query->select('ar.child_id as id')
                ->from('users as u')
                ->join('agent_relation as ar', 'u.id', '=', 'ar.parent_id')
                ->where(function ($query) use ($id) {
                    $query->orWhere('ar.parent_id', $id);
                    $query->orWhere('ar.child_id', $id);
                });
        });
    }
}
